package com.jiantong.web.open.app.news;

import com.jiantong.news.model.Thinktank;
import com.jiantong.news.service.ThinktankReadService;
import com.jiantong.news.service.ThinktankWriteService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * 养殖智库
 * Created by mz03 on 2017/4/13.
 * @author :zyp
 */
@Slf4j
@OpenBean
public class ThinktankApis {
    @RpcConsumer
    private ThinktankReadService thinktankReadService;

    @RpcConsumer
    private ThinktankWriteService thinktankWriteService;

    @OpenMethod(
            key = "find.thinktand.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public Thinktank findById(Long id){
        Response<Thinktank> response = thinktankReadService.finById(id);
        if (!response.isSuccess()){
            log.error("failed to find thinktand read by id = {}, cause : {}", id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    @OpenMethod(
            key = "create.thinktand",
            paramNames = {"thinktand"},
            httpMethods = RequestMethod.POST
    )
    public Long create(Thinktank thinktand){
        Response<Long> response = thinktankWriteService.create(thinktand);
        if(!response.isSuccess()){
            log.error("failed to create thinktand read by thinktand = {}, cause : {}", thinktand, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    @OpenMethod(
            key = "query.thinktanks.by.type",
            paramNames = {"type"},
            httpMethods = RequestMethod.GET
    )
    public List<Thinktank> findByType(Integer type){
        Response< List<Thinktank>> response = thinktankReadService.finByType(type);
        if (!response.isSuccess()){
            log.error("failed to find List<thinktand> read by type = {}, cause : {}", type, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();

    }

    @OpenMethod(
            key = "find.paging.thinktank",
            paramNames = {"pageNo","pageSize","types"},
            httpMethods = RequestMethod.GET
    )
    public Paging<Thinktank> pagingThinktank(Integer pageNo, Integer pageSize, List<Integer> types){
        Response<Paging<Thinktank>> pagingResponse = thinktankReadService.pagingThinktank(pageNo,pageSize,types);
        if(!pagingResponse.isSuccess()){
            log.error("failed to find paging thinktank types {}, cause : {}", types, pagingResponse.getError());
            throw new OPServerException(pagingResponse.getError());
        }
        return pagingResponse.getResult();
    }

    @OpenMethod(
            key = "find.paging.thinktank.keyword",
            paramNames = {"pageNo","pageSize","keyword","type"},
            httpMethods = RequestMethod.GET
    )
    public Paging<Thinktank> pagingThinktankByKeyWord(Integer pageNo, Integer pageSize,String keyword,Integer type){
        Response<Paging<Thinktank>> pagingResponse = thinktankReadService.pagingThinktankKeyword(pageNo,pageSize,keyword,type);
        if(!pagingResponse.isSuccess()){
            log.error("failed to find paging thinktank keyword {}, cause : {}", keyword, pagingResponse.getError());
            throw new OPServerException(pagingResponse.getError());
        }
        return pagingResponse.getResult();
    }
}
